package com.ctg.itrdc.imageconsole.control.depend;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.imageconsole.data.entity.DockerMicroAppRefer;
import com.ctg.itrdc.imageconsole.service.IServiceDependService;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;
import com.ctg.itrdc.paspsdk.common.utils.type.StringUtils;

/**
 * 服务依赖相关接口
 * 
 * @author wupiao
 *
 */
@Controller
@RequestMapping("/serviceDependence")
public class ServiceDependController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IServiceDependService serviceDependService;

	@RequestMapping(value = "/queryList", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryList(
			@RequestParam(value = "name", required = false) String name,
			@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
		try {
			DockerMicroAppRefer appRefer = new DockerMicroAppRefer();
			if (StringUtils.isNotEmpty(name)) {
				name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); 
				appRefer.setName(name);
			}
			PageInfo<DockerMicroAppRefer> pageInfo = serviceDependService.queryDependencePageInfo(appRefer, pageNum,
					pageSize);
			if (pageInfo.getList().size() > 0) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
			}
			PageInfo<ClusterConfig> pageInfoForNull = new PageInfo<>();
			pageInfoForNull.setList(new ArrayList<ClusterConfig>());

			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfoForNull);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	
	@RequestMapping(value = "/queryDetail", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryDetail(@RequestParam("id") Long id) {
		try {
			DockerMicroAppRefer appRefer = serviceDependService.queryDependenceDetail(id);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, appRefer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	
	/*@RequestMapping(value = "/queryApps", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryApps(@RequestParam(value="name", required = false) String name) {
		try {
			List<String> apps = serviceDependService.getZkApps(name);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, apps);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}*/

	/**
	 * 保存服务依赖关系到数据库
	 * 
	 * @param appRefer
	 *            依赖关系实体类
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult save(@RequestBody DockerMicroAppRefer appRefer) {
		try {
			serviceDependService.saveDependence(appRefer);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult delete(@RequestBody DockerMicroAppRefer appRefer) {
		try {
			serviceDependService.deleteDependence(appRefer.getId());
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
}
